package com.czg.mybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czg.mybatisplus.entity.Book;
import com.czg.mybatisplus.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/books")
public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping
    public String books(Model model){
        List<Book> list=bookService.list();
        model.addAttribute("list",list);
        return "books";
    }

    @PostMapping
    public String searchBooks(Model model,Book book){
       List<Book>list= bookService.searchBooks(book);
       model.addAttribute("list",list);
       model.addAttribute("name",book.getName());
        model.addAttribute("category",book.getCategory());
        model.addAttribute("author",book.getAuthor());

       return "books";
    }

    @GetMapping("/page")
    public String page(@RequestParam(defaultValue = "1")Integer pageNum, Model model){
        IPage page=bookService.getPage(pageNum);
        model.addAttribute("page",page);
        return "bookPage";
    }
}
